<?php

namespace Tasker\Forms\Controls;

use \Nette\Forms\Controls\BaseControl,
	\Nette\Forms\Controls\SelectBox,
	\Nette\Forms\Container,
	\Nette\Utils\Html;


class CheckboxDetailList extends BaseControl
{

	/** @var array */
	private $checkboxItems;
	/** @var array */
	private $detailItems;
	/** @var Nette\Web\Html  separator element template */
	protected $outerSeparator;
	/** @var Nette\Web\Html  separator element template */
	protected $innerSeparator;
	/** @var Nette\Web\Html  container element template */
	protected $container;

	/**
	 * Form container extension method. Do not call directly.
	 *
	 * @param FormContainer $form
	 * @param string $name
	 * @param string $label
	 * @param array $members
	 * @param array $roles
	 * @return CheckboxList
	 */
	public static function addCheckboxDetailList($form, $name, $label = '', array $members = NULL, $roles = NULL)
	{
		return $form[$name] = new self($label, $members, $roles);
	}

	/**
	 * @param string $label
	 * @param array $members  Members from which to choose
	 * @param array $roles  Roles from which to choose
	 */
	public function __construct($label, array $members = NULL, $roles = NULL)
	{
		parent::__construct($label);
		$this->control->type = 'checkbox';
		$this->container = Html::el();
		$this->outerSeparator = Html::el('div');
		$this->innerSeparator = Html::el('br');
		$this->checkboxItems = $members;
		$this->detailItems = $roles;
	}

	/**
	 * Generates control's HTML element.
	 *
	 * @return Nette\Web\Html
	 */
	public function getControl()
	{
		$container = clone $this->container;
		$values = $this->value === NULL ? NULL : (array) $this->getValue();

		foreach ($this->checkboxItems as $checkboxItemId => $checkboxItem)
		{
			$containerItem = clone $this->outerSeparator;
			$containerItem->class = 'member-item item-' . $checkboxItemId;

			$control = parent::getControl();
			$control->name .= '[' . $checkboxItemId . '][member]';
			$control->id .= '-member-' . $checkboxItemId;
			$control->checked = isset($values[$checkboxItemId]['member']);
			$control->class = 'member-checkbox';
			$control->value = $checkboxItemId;
			$label = Html::el('label');
			$label->for = $control->id;
			$label->setText($checkboxItem);

			$containerItem->add((string) $control . (string) $label . (string) $this->innerSeparator);

			$control = parent::getControl();
			$control->setName('select');
			$control->size = 1;
			$control->name .= '[' . $checkboxItemId . '][role]';
			$control->id .= '-role-' . $checkboxItemId;
			$control->class = 'member-select';

			foreach ($this->detailItems as $detailItemId => $detailItem)
			{
				$option = Html::el('option');
				$option->value = $detailItemId;
				$option->selected = isset($values[$checkboxItemId]['member']) ? ($values[$checkboxItemId]['role'] == $detailItemId) : false;
				$option->setText($detailItem);
				$control->add($option);
			}
			$containerItem->add((string) $control);
			$container->add($containerItem);
		}

		return $container;
	}

	/**
	 * Returns selected members with roles. NULL means nothing have been checked.
	 *
	 * @return mixed
	 */
	public function getValue()
	{
		if (is_array($this->value))
		{
			foreach ($this->value as $key => $value)
			{
				if (!isset($value['member']))
				{
					unset($this->value[$key]);
				}
			}
			return $this->value;
		} else
		{
			return NULL;
		}
	}

	/**
	 * Returns members from which to choose.
	 *
	 * @return array
	 */
	public function getCheckboxItems()
	{
		return $this->checkboxItems;
	}

	/**
	 * Sets members from which to choose.
	 *
	 * @param array $members
	 * @return CheckboxDetailList  provides a fluent interface
	 */
	public function setCheckboxItems($members)
	{
		$this->checkboxItems = $members;
		return $this;
	}

	/**
	 * Returns roles from which to choose.
	 *
	 * @return array
	 */
	public function getDetailItems()
	{
		return $this->detailItems;
	}

	/**
	 * Sets roles from which to choose.
	 *
	 * @param array $roles
	 * @return CheckboxDetailList  provides a fluent interface
	 */
	public function setDetailItems($roles)
	{
		$this->detailItems = $roles;
	}

	/**
	 * Returns separator HTML element template.
	 *
	 * @return Nette\Web\Html
	 */
	public function getOuterSeparatorPrototype()
	{
		return $this->outerSeparator;
	}

	/**
	 * Returns separator HTML element template.
	 *
	 * @return Nette\Web\Html
	 */
	public function getInnerSeparatorPrototype()
	{
		return $this->innerSeparator;
	}

	/**
	 * Returns container HTML element template.
	 *
	 * @return Nette\Web\Html
	 */
	public function getContainerPrototype()
	{
		return $this->container;
	}

	public function addRule($operation, $message = NULL, $arg = NULL)
	{
		throw new \Nette\NotImplementedException("Rules are not implemented for CheckboxDetailList.");
	}

	public function addCondition($operation, $value = NULL)
	{
		throw new \Nette\NotImplementedException("Conditions are not implemented for CheckboxDetailList");
	}

	public function addConditionOn(\Nette\Forms\IControl $control, $operation, $value = NULL)
	{
		throw new \Nette\NotImplementedException("Conditions are not implemented for CheckboxDetailList");
	}

}
